//
//  UIView+Ext.swift
//  ESVpn
//
//  Created by nkl on 2023/6/28.
//

import Foundation
import UIKit

extension UIView {
    public static func simpleView(color: UIColor = .white) -> UIView {
        let view = UIView()
        view.backgroundColor = color
        return view
    }

    /// 设置圆角
    /// - Parameters:
    ///   - corner: <#corner description#>
    ///   - radii: <#radii description#>
    public func setRectCorner(corner: UIRectCorner, radii: CGSize) {
        let maskPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: corner, cornerRadii: radii)

        let maskLayer = CAShapeLayer()
        maskLayer.frame = bounds
        maskLayer.path = maskPath.cgPath
        layer.mask = maskLayer
    }
}

extension UIView {
    public var kl_left: CGFloat {
        frame.origin.x
    }

    public func changeLeft(num: CGFloat) {
        var mframe = frame
        mframe.origin.x = num
        frame = mframe
    }

    public var kl_top: CGFloat {
        frame.origin.y
    }

    public func changeTop(num: CGFloat) {
        var mframe = frame
        mframe.origin.y = num
        frame = mframe
    }

    public var kl_right: CGFloat {
        frame.origin.x + frame.size.width
    }

    public func changeRight(num: CGFloat) {
        var mframe = frame
        mframe.origin.x = num - frame.size.width
        frame = mframe
    }

    public var kl_bottom: CGFloat {
        frame.origin.y + frame.size.height
    }

    public func changeBottom(num: CGFloat) {
        var mframe = frame
        mframe.origin.y = num + frame.size.height
        frame = mframe
    }

    public var kl_width: CGFloat {
        frame.size.width
    }

    public func changeWidth(num: CGFloat) {
        var mframe = frame
        mframe.size.width = num
        frame = mframe
    }

    public var kl_height: CGFloat {
        frame.size.height
    }

    public func changeHeight(num: CGFloat) {
        var mframe = frame
        mframe.size.height = num
        frame = mframe
    }

    public var kl_size: CGSize {
        frame.size
    }

    public func changeSize(size: CGSize) {
        var mframe = frame
        mframe.size = size
        frame = mframe
    }

    public var kl_origin: CGPoint {
        frame.origin
    }

    public func changeOrigin(origin: CGPoint) {
        var mframe = frame
        mframe.origin = origin
        frame = mframe
    }

    public var kl_centerX: CGFloat {
        center.x
    }

    public func changeCenterX(num: CGFloat) {
        center = CGPoint(x: num, y: center.y)
    }

    public var kl_centerY: CGFloat {
        center.y
    }

    public func changeCenterY(num: CGFloat) {
        center = CGPoint(x: center.x, y: num)
    }
}

extension UIImageView {
    public static func simpleImage(imageName: String) -> UIImageView {
        if imageName.isEmpty {
            return UIImageView()
        }
        return UIImageView(image: UIImage(named: imageName))
    }
}
